package lesson.student;

import com.github.pagehelper.PageInfo;
import lesson.base.BaseServlet;
import lesson.base.paramresolve.UploadFile;
import lesson.permission.Role;
import lesson.user.User;
import org.apache.poi.ss.usermodel.Workbook;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;

@WebServlet("/student/*")
@Role(User.ROLE_ADMIN)
public class StudentServlet extends BaseServlet {

    private StudentService studentService = new StudentService();

    /**
     * 教师管理的首页
     */
    public void index() {
        forwardJsp("/pages/student/index");
    }

    /**
     * 教师管理的编辑页面
     */
    public void edit(Integer id) {
        if (id != null) {
            Student student = studentService.getById(id);
            getRequest().setAttribute("student", student);
        }
        forwardJsp("/pages/student/edit");
    }


    public void export(String keywords) throws IOException {
        Workbook workbook = studentService.export(keywords);


        getResponse().setContentType("application/vnd.ms-excel");
        String recommendedName = new String("学生数据.xls".getBytes(),"iso_8859_1");//设置文件名称的编码格式
        getResponse().setHeader("Content-Disposition", "attachment; filename="+recommendedName);//
        workbook.write(getResponse().getOutputStream());
    }


    /**
     * 查询学生
     */
    public void list(String keywords, Integer pageNumber, Integer pageSize) throws ServletException, IOException {
        PageInfo<Student> list = studentService.listByPage(keywords, pageNumber, pageSize);
        writeJSON(true, null, list);
    }

    /**
     * 查询某个班的学生列表
     * @param classId
     * @throws ServletException
     * @throws IOException
     */
    public void listByClass(Integer classId) throws ServletException, IOException {
        if(classId== null){
            writeJSON(false,"请选择班级");
            return;
        }
        writeJSON(true,null,studentService.list(null,null,classId));
    }


    /**
     * 删除教师
     * @param id
     */
    public void del(Integer id) throws ServletException, IOException {
        String result = studentService.del(id);
        if (result == null) {
            writeJSON(true);
        } else {
            writeJSON(false, result);
        }
    }


    /**
     * 保存教师
     * @throws ServletException
     * @throws IOException
     */
    public void save(Student student) throws ServletException, IOException {

        String result = studentService.saveStudent(student);
        if (result == null) {
            writeJSON(true);
        } else {
            writeJSON(false, result);
        }
    }

    /**
     * 进入导入页面
     */
    public void initImport() {
        forwardJsp("/pages/student/import.jsp");
    }

    /**
     * 下载导入模板
     * @throws IOException
     */
    public void downloadModel() throws IOException {
        Workbook workbook = studentService.getDownloadModel();
        getResponse().setContentType("application/vnd.ms-excel");
        String recommendedName = new String("学生导入模板.xls".getBytes(),"iso_8859_1");//设置文件名称的编码格式
        getResponse().setHeader("Content-Disposition", "attachment; filename="+recommendedName);//
        workbook.write(getResponse().getOutputStream());
    }


    public void upload(UploadFile file) throws ServletException, IOException {
        String result = studentService.upload(file);
        writeJSON(result == null, result);


    }

}
